<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 猴子爬山（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>一天一只顽猴想去从山脚爬到山顶&#xff0c;途中经过一个有个N个台阶的阶梯&#xff0c;但是这猴子有一个习惯&#xff1a;</p> 
<p>每一次只能跳1步或跳3步&#xff0c;试问猴子通过这个阶梯有多少种不同的跳跃方式&#xff1f;</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入只有一个整数N&#xff08;0&lt;N&lt;&#61;50&#xff09;此阶梯有多少个台阶。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出有多少种跳跃方式&#xff08;解决方案数&#xff09;。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">50</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">122106097</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>这题是一道经典的分治算法题、以及动态规划基础题。</p> 
<p>这题既可以使用分治递归来自顶向下地求解&#xff0c;也可以使用动态规划递推地自底向上求解。</p> 
<p>但是动态规划的效率更高。</p> 
<p></p> 
<p>这题和fibnaci数列很像&#xff0c;递归公式如下&#xff1a;</p> 
<ul><li>jump(1) &#61; 1</li><li>jump(2) &#61; 1</li><li>jump(3) &#61; 2</li><li>jump(n) &#61; jump(n-1) &#43; jump(n-3),  n&gt;3</li></ul> 
<p>状态转移方程如下&#xff1a;</p> 
<ul><li>dp[0] &#61; 0</li><li>dp[1] &#61; 1</li><li>dp[2] &#61; 1</li><li>dp[3] &#61; 2</li><li>dp[i] &#61; dp[i-1] &#43; dp[i-3],  i&gt;3</li></ul> 
<p></p> 
<p>上面递归公式和状态转移方程的规律发现如下</p> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:108px;">台阶数</td><td style="width:118px;">跳跃方式</td><td style="width:65px;">几种跳跃方式</td></tr><tr><td style="width:108px;">1</td><td style="width:118px;">1</td><td style="width:65px;">1</td></tr><tr><td style="width:108px;">2</td><td style="width:118px;">11</td><td style="width:65px;">1</td></tr><tr><td style="width:108px;">3</td><td style="width:118px;"> <p>111</p> <p>3</p> </td><td style="width:65px;">2</td></tr><tr><td style="width:108px;">4</td><td style="width:118px;"> <p>1111</p> <p>13</p> <p>31</p> </td><td style="width:65px;">3</td></tr><tr><td style="width:108px;">5</td><td style="width:118px;"> <p>11111</p> <p>113</p> <p>131</p> <p>311</p> </td><td style="width:65px;">4</td></tr><tr><td style="width:108px;">6</td><td style="width:118px;"> <p>111111</p> <p>1113</p> <p>1131</p> <p>1311</p> <p>3111</p> <p>33</p> </td><td style="width:65px;">6</td></tr><tr><td style="width:108px;">7</td><td style="width:118px;"> <p>1111111</p> <p>11113</p> <p>11131</p> <p>11311</p> <p>13111</p> <p>31111</p> <p>133</p> <p>313</p> <p>331</p> </td><td style="width:65px;">9</td></tr></tbody></table> 
<p></p> 
<h4>Java算法源码</h4> 
<p>分治递归求解&#xff08;带缓存优化&#xff09;</p> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  static int[] cache &#61; new int[51];

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int n &#61; sc.nextInt();

    Arrays.fill(cache, -1);
    cache[0] &#61; 0;
    cache[1] &#61; 1;
    cache[2] &#61; 1;
    cache[3] &#61; 2;

    System.out.println(recursive(n));
  }

  public static int recursive(int n) {
    if (cache[n] !&#61; -1) return cache[n];
    cache[n] &#61; recursive(n - 1) &#43; recursive(n - 3);
    return cache[n];
  }
}
</code></pre> 
<p>动态规划算法求解</p> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int n &#61; sc.nextInt();
    System.out.println(getResult(n));
  }

  public static int getResult(int n) {
    int[] dp &#61; new int[n &#43; 1];

    if (n &gt;&#61; 1) dp[1] &#61; 1;
    if (n &gt;&#61; 2) dp[2] &#61; 1;
    if (n &gt;&#61; 3) dp[3] &#61; 2;

    for (int i &#61; 4; i &lt;&#61; n; i&#43;&#43;) {
      dp[i] &#61; dp[i - 1] &#43; dp[i - 3];
    }

    return dp[n];
  }
}
</code></pre> 
<p> </p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<p>分治递归求解&#xff08;带缓存优化&#xff09;</p> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const n &#61; parseInt(line);
  console.log(recursive(n));
});

const cache &#61; [0, 1, 1, 2];
function recursive(n) {
  if (cache[n]) return cache[n]; // n &gt; 0
  cache[n] &#61; recursive(n - 1) &#43; recursive(n - 3);
  return cache[n];
}
</code></pre> 
<p></p> 
<p>动态规划算法求解</p> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const n &#61; parseInt(line);
  console.log(getResult(n));
});

function getResult(n) {
  const dp &#61; new Array(n &#43; 1).fill(0);
  if (n &gt;&#61; 1) dp[1] &#61; 1;
  if (n &gt;&#61; 2) dp[2] &#61; 1;
  if (n &gt;&#61; 3) dp[3] &#61; 2;

  for (let i &#61; 4; i &lt;&#61; n; i&#43;&#43;) {
    dp[i] &#61; dp[i - 1] &#43; dp[i - 3];
  }

  return dp[n];
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<p>分治递归求解&#xff08;带缓存优化&#xff09;</p> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())

cache &#61; [-1] * 51
cache[0] &#61; 0
cache[1] &#61; 1
cache[2] &#61; 1
cache[3] &#61; 2


# 算法入口
def recursive(n):
    if cache[n] !&#61; -1:
        return cache[n]

    cache[n] &#61; recursive(n - 1) &#43; recursive(n - 3)
    return cache[n]


# 调用算法
print(recursive(n))
</code></pre> 
<p>动态规划算法求解</p> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())


# 算法入口
def getResult():
    dp &#61; [0]*(n&#43;1)

    if n &gt;&#61; 1:
        dp[1] &#61; 1
    if n &gt;&#61; 2:
        dp[2] &#61; 1
    if n &gt;&#61; 3:
        dp[3] &#61; 2

    for i in range(4, n&#43;1):
        dp[i] &#61; dp[i-1] &#43; dp[i-3]

    return dp[n]


# 调用算法
print(getResult())
</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>